/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.player;

import cz.fi.muni.diplomka.common.enumeration.ColorType;
import cz.fi.muni.diplomka.common.enumeration.TitleType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.Player;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.servicelayer.util.Crypto;
import cz.muni.fi.diplomka.webapp.city.CityPage;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class RegistrationPage extends WebPage {

    private PlayerManager playerManager = new PlayerManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private static final List<String> GENDER = Arrays.asList(new String[]{
        "Sir", "Lady"});
    private static final List<String> COLOR = Arrays.asList(new String[]{
        "Red", "Yellow", "Green", "Blue"});
    private String gender = "Sir"; // Sir is selected by default
    private String color = "Red";   // Red is selected by default

    public RegistrationPage() {
        add(new FeedbackPanel("feedback"));


        final RequiredTextField<String> nickName = new RequiredTextField<>("nickName", Model.of(""));

        final PasswordTextField password = new PasswordTextField("password", Model.of(""));

        final PasswordTextField retypePassword = new PasswordTextField("retypePassword", Model.of(""));

        final DropDownChoice<String> gender = new DropDownChoice<>("gender",
                new PropertyModel<String>(this, "gender"), GENDER);

        final DropDownChoice<String> color = new DropDownChoice<>("color",
                new PropertyModel<String>(this, "color"), COLOR);

        final RequiredTextField<String> cityName = new RequiredTextField<>("city", Model.of(""));


        Form<?> form = new Form<Void>("form") {
            @Override
            protected void onSubmit() {
                
                if(playerManager.findPlayerByLogin(nickName.getValue()).getId() != null){
                    error("Name already taken");
                    return;
                }
                if(cityManager.getCityByName(cityName.getValue()).getId() != null){
                    error("City name already taken");
                    return;
                }
                
                Player player = new Player();
                player.setNickName(nickName.getValue());
                
                try {
                    player.setHashedPassword(Crypto.encode(password.getValue()));
                } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
                    Logger.getLogger(RegistrationPage.class.getName()).log(Level.SEVERE, null, ex);
                }
                switch (color.getValue()) {
                    case "0":
                        player.setColor(ColorType.Red);
                        break;
                    case "1":
                        player.setColor(ColorType.Yellow);
                        break;
                    case "2":
                        player.setColor(ColorType.Green);
                        break;
                    case "3":
                        player.setColor(ColorType.Blue);
                        break;
                }
                switch (gender.getValue()) {
                    case "0":
                        player.setTitle(TitleType.Sir);
                        break;
                    case "1":
                        player.setTitle(TitleType.Lady);
                        break;
                }

                playerManager.createPlayer(player);
                Long id = playerManager.findPlayerByLogin(player.getNickName()).getId();
                City city = new City();
                
                city.setName(cityName.getValue());
                city.setPlayerId(id);
                cityManager.createCity(city);

                PageParameters pageParameters = new PageParameters();
                pageParameters.add("id", id.toString());
                setResponsePage(CityPage.class, pageParameters);
            }
        };

        add(form);
        form.add(nickName);
        form.add(password);
        form.add(retypePassword);
        form.add(new EqualPasswordInputValidator(password, retypePassword));
        form.add(color);
        form.add(gender);
        form.add(cityName);
    }
}
